var express = require('express');
var router=express.Router();

const {request, response} = require("express");
const{ Sequelize,DataTypes } = require('sequelize');
const jwt = require("jsonwebtoken");
const orm = require('../util/connect').orm()

const goods = orm.import('../sequelize/models/goods.model')
const order = orm.import('../sequelize/models/order.model');
//根据订单id查订单
router.post('/OrderById',(req,res)=>{
    const orderid = req.body.order_id;
    order.findOne({
        where:{
            order_id:orderid
        },
        attributes:['order_id','order_type','order_finish_time','order_status','estimate']
    }).then(result =>{
        if(result !=null){
            res.send({
                code:200,
                msg:"查询成功",
                data:result
            })
        }else{
            res.send({
                code:402,
                msg:'订单号输入错误'
            })
        }
    });
});
//根据手机号查订单
router.post('/OrderByTel',(req,res)=>{
    const tel = req.user['user.tel'];
    const type = req.body.type;
    console.log(req.body);
    if(type == '我寄的'){
        order.findAll({
            where:{
                deuser_tel:tel
            },
            attributes:['order_id','order_type','order_finish_time','order_status','estimate']
        }).then(result =>{
            if(result !=null){
                console.log(result);
                res.send({
                    code:200,
                    msg:"查询成功",
                    data:result
                })
            }else{
                res.send({
                    code:402,
                    msg:'订单为空'
                })
            }
        });
    }if(type == '我收的'){
        order.findAll({
            where:{
                reuser_tel:tel
            },
            attributes:['order_id','order_type','order_finish_time','order_status','estimate']
        }).then(result =>{
            if(result !=null){
                res.send({
                    code:200,
                    msg:"查询成功",
                    data:result
                })
            }else{
                res.send({
                    code:402,
                    msg:'订单为空'
                })
            }
        });
    }
});
//查询所有订单
router.get('/AllOrder',(req,res)=>{
    order.findAll()
        .then(result =>{
            if(result !=null){
                res.send({
                    code:200,
                    msg:"查询成功",
                    data:result
                })
            }else{
                res.send({
                    code:401,
                    msg:'订单为空'
                })
            }
        });
});
//添加新订单
router.post('/AddOrder',(req,res)=>{
    const data=req.body.formData;
    console.log(data);
    order.create({
        reuser_name:data.reuser_name,
        reuser_addr:data.reuser_addr,
        reuser_tel:data.reuser_tel,
        deuser_name:data.deuser_name,
        deuser_addr:data.deuser_addr,
        deuser_tel:data.deuser_tel,
        order_type:data.order_type,
        price:data.price,
        goods:{
            goods_id:0,
            goods_type:data.goods_type,
            weight:data.goods_weight,
            remark:data.remark
        }
    },{
        include:{
            raw:true,
            model:goods,
            as:'goods'
        }
    }).then(result =>{
        if(result !=null){
            res.send({
                code:200,
                msg:"添加成功",
                data:result
            })
        }else{
            res.send({
                code:402,
                msg:'添加失败'
            })
        }
    })
})
//更新订单信息
router.post('/updateOrder',(req,res)=>{
    const status=req.body.status;
    const newOrder = order.update({
        order_status:status
    })
    res.sendStatus(200);
})
//获取订单货物
router.post('/goodsById',(req,res)=>{
    const orderid = req.body.order_id;
    order.findOne({
        where:{
            order_id:orderid
        },
        include:[{
            model:goods,
            as:goods
        }],
        raw:true
    }).then(result=>{
        if(result != null){
            res.send({
                code:200,
                data:result
            })
        }
    }).catch(error=>{
        res.send({
            code:400,
            msg:'查询货物失败'
        })
    })
})
//删除订单
router.delete('/delete',(req,res)=>{
    const orderid=req.body.order_id;
    console.log(orderid)
    order.destroy({where:{order_id:orderid}})
        .then(result => {
            if (result != 0){
                res.send({
                    code: 200,
                    msg: '已成功删除'
                })
            }
            else{
                res.send({
                    code: 500,
                    msg: '删除失败'
                })
            }
        })
})
//获取订单数量
router.get('/orderNum',async (req,res)=>{
    const amount =await order.findAll({
        attributes:[[orm.fn('COUNT',orm.col('order_finish_time')),'month']],
        group: [orm.fn('DATE_FORMAT',orm.col('order_finish_time'), '%Y-%m') ]
    })
    res.send({
        code:200,
        data:amount
    })
})
//获取订单金额
router.get('/orderSum',async (req,res)=>{
    const total = await order.findAll({
        attributes:[[orm.fn('SUM',orm.col('price')),'sum']],
        group: [orm.fn('DATE_FORMAT',orm.col('order_finish_time'), '%Y-%m') ]
    })
    res.send({
        code:200,
        data:total
    })
})
module.exports=router;
